﻿using System;
using System.Text;
using PostRequestFactory.Core.Interfaces;

namespace PostRequestFactory.Core.Parameters
{
    /// <summary>
    /// This class is used for adding text content to the post request.
    /// TextRequestParameter can be used only with requests with multipart/form-data content type.
    /// </summary>
    public class TextRequestParameter : IRequestParameter
    {
        private string _content;
        private readonly Encoding _initialEncoding;

        /// <summary>
        /// Will include 'Content-Type:' option completed with 
        /// ContentType property value.
        /// Default is false.
        /// </summary>
        public bool IncludeContentTypeHeaderInTheBodySection { get; set; }

        /// <summary>
        /// Content will be provided as base64 string.
        /// Default is true.
        /// </summary>
        public bool TransportAsBase64 { get; private set; }

        /// <summary>
        /// Name of the field/variable to which corresponds the content
        /// </summary>
        public string Name { get; private set; }

        /// <summary>
        /// Default value will be "text/plain",
        /// </summary>
        public string ContentType { get; set; }

        public string ContentDisposition { get { return "form-data"; } }

        /// <summary>
        /// Content as it was provided or
        /// content converted to base64 if TransportAsBase64 = true
        /// </summary>
        public string Content {

            get {

                if (TransportAsBase64)
                {
                    return Convert.ToBase64String(_initialEncoding.GetBytes(_content));
                }

                return _content;
            }

            set { _content = value; }
        }

        public RequestParameterType ParameterType
        {
            get { return RequestParameterType.Text; }
        }

        #region Ctors
        
        /// <summary>
        /// Creates a new instance of a TextRequestParameter.
        /// </summary>
        /// <param name="name">Control name.</param>
        /// <param name="content">Control content/value.</param>
        /// <param name="transportAsBase64">Convert content to base64. Default value is false.</param>
        /// <param name="currentContentEncoding">Encoding used by the current content.</param>
        public TextRequestParameter(string name, string content, bool transportAsBase64, Encoding currentContentEncoding)
        {
            Name = name;
            Content = content;
            TransportAsBase64 = transportAsBase64;
            _initialEncoding = currentContentEncoding;

            ContentType = "text/plain";
        }

        /// <summary>
        /// Creates a new instance of a TextRequestParameter.
        /// </summary>
        /// <param name="name">Control name.</param>
        /// <param name="content">Control content/value.</param>
        public TextRequestParameter(string name, string content)
            : this(name, content, false, Encoding.UTF8)
        {
        } 

        #endregion

    }
}
